<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/Admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/Admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link href="./Public/Admin/Flat/docs/assets/css/demo.css" rel="stylesheet">
	    <link rel="shortcut icon" href="./Public/Admin/Flat/img/favicon.ico">
	    <script type="text/javascript" src="Public/uploadify/jquery-1.8.2.min.js"></script>
    	<script type="text/javascript" src="Public/uploadify/jquery.uploadify.min.js"></script>
    	<link rel="stylesheet" type="text/css" href="Public/uploadify/uploadify.css">
    	<!--载入百度编辑器-->
	    	<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.config.js"></script>
    		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.all.min.js"> </script>
    		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
		<style type="text/css">
		.form-group{
			padding: 0;
			margin: 0;
		}
		</style>
	</head>
	<script>
	// alert(1);
		$(function (){
			$('#cate').change(function(){
				//当selected值发生变化时，获得值，进行异步操作
				var zhi=$(this).val();
				// alert(zhi);
				$.ajax({
					type:"post",
					url:"index.php?m=Admin&c=Goods&a=sel",
					data:{f:zhi},
					dataType:'json',
					success:function(phpData){
						// console.log(phpData);
						var attr='';var spec='';
						$.each(phpData,function(k,v){
							// console.log(v.tavalue);
							// 添加属性
							if(v.attr_type==0){
								attr += '<tr class="info">';
								attr += '<td>'+v.taname+'</td>';
								attr += '<td>';
								attr += '<select name="attr['+v.taid+']" id="">';
								attr += '<option value="">请选择</option>';
									 $.each(v.tavalue, function(kk,vv) {    
										attr += '<option value="'+vv+'">'+vv+'</option>';
									 });
								attr += '</select>';
								attr += '</td></tr>';
								    
							}else{
								//遍历规格
								 
									spec += '<tr class="info">';
									spec += '<td>'+v.taname+'</td>';
									spec += '<td>';
									spec += '<select name="spec[value]['+v.taid+'][]" id="">';
									spec += '<option value="">请选择</option>';
										$.each(v.tavalue, function(kk,vv) {    
											spec += '<option value="'+vv+'">'+vv+'</option>';                                                          
										});
									spec += '</select></td>';
									spec += '<td>附加价格：<input type="text" name="spec[added]['+v.taid+'][]" id=""/></td>';
									spec += '<td>';
									spec += '<a href="javascript:;" class="btn btn-primary addSpec">添加规格</a>';
									spec += '</td></tr>';

									

							}
							
						})

						 $("#spec").html(spec);   $("#attr").html(attr);
					}
					
				})
			})
			//点击添加规格
			$('.addSpec').live('click',function(){
				//找到当前的父级的tr
				var trObj = $(this).parents('tr');
				//克隆一个
				var tr = trObj.clone();
				tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
				//再插入到当前父级的下面
				trObj.after(tr);
				
			})
			$('.removeSpec').live('click',function(){
				$(this).parents('tr').remove();
			})

		})
	</script>
	<body>
		<form action="" method="post" enctype="multipart/form-data">
		<div class="alert alert-success">编辑商品</div>
		<div class="alert alert-success" style="width: 200px;">基本信息</div>
		<div class="form-group">
			<label for="exampleInputEmail1">商品名称</label>
			<input id="exampleInputEmail1" class="form-control" type="text"  name="gname" value="{{$goodsData['gname']}}">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">商品描述</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['des']}}"  name="des">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">所属分类</label>
			<select id="cate" name="category_cid" class="form-control">
				<option value="">请选择</option>
				{{foreach from="$cateData" value="$v"}}
					<option value="{{$v['cid']}}" {{if value="$goodsData['category_cid'] eq $v['cid']"}} selected {{endif}}>{{$v['_name']}}</option>
				{{endforeach}}
			</select>
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">所属品牌</label>
				<select id="cate" name="brand_bid" class="form-control">
				<option value="">请选择</option>
				{{foreach from="$brandData" value="$v"}}
					<option value="{{$v['bid']}}" <?php if($v['bid']==$goodsData['brand_bid']): ?>selected<?php endif ?>>{{$v['bname']}}</option>
				{{endforeach}}
			</select>
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">单位</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['unit']}}"  name="unit">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">库存</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['stock']}}"  name="stock">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">市场价</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['re_price']}}"  name="re_price">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">商城价</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['price']}}"  name="price">
		</div>
		<div class="form-group">
			<label for="exampleInputEmail1">货号</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['number']}}"  name="number">
		</div>
<!--		<div class="form-group">-->
<!--			<label for="exampleInputEmail1">上架时间</label>-->
<!--			<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类排序"  name="bsort">-->
<!--		</div>-->
		<div class="form-group">
			<label for="exampleInputEmail1">点击次数</label>
			<input id="exampleInputEmail1" class="form-control" type="text" value="{{$goodsData['click']}}" name="click">
			<input type="hidden" name="gid" value="{{$goodsData['gid']}}">
		</div>
		<p class="btn btn-primary" id="gattr">商品属性</p>
		<table class="table table-bordered table-hover" id='attr' class="info">
				{{foreach from="$data" value="$v"}}
				{{if value="$v['attr_type'] eq 0"}}
				<tr class="info">
				    <td>{{$v['taname']}}</td>
					<td>
						<select name="attr[{{$v['taid']}}][]" >
							<option value="">请选择</option>
							{{foreach from="$v['tavalue']" value="$value"}}
							<option value="{{$value}}" {{if value="$v['gavalue'] eq $value"}}selected {{endif}}>{{$value}}</option>
							{{endforeach}}
						</select>
					</td>
				</tr>
				{{endif}}
				{{endforeach}}
			</table>
		<p class="btn btn-primary">商品规格</p>
		<table class="table table-bordered table-hover" id='spec' class="info">
				{{foreach from="$data" value="$v"}}
				{{if value="$v['attr_type'] eq 1"}}
				<?php $taid[]=$v['taid'] ?>
				<tr class="info">
				    <td>{{$v['taname']}}</td>
					<td>
						<select name="spec[value][{{$v['taid']}}][]" >
							<option value="">请选择</option>
							{{foreach from="$v['tavalue']" value="$value"}}
							<option value="{{$value}}" {{if value="$v['gavalue'] eq $value"}}selected {{endif}}>{{$value}}</option>
							{{endforeach}}
						</select>
					</td>
					<td>
						附加价格: <input type="text" name="spec[added][{{$v['taid']}}][]" id="" value="<?php if(isset($v['extra_price'])){echo ($v['extra_price']);}else{echo 0;}  ; ?>">
					</td>
					<td>
						<a href="javascript:;" class="btn btn-info removeSpec goodsAttr{{$v['taid']}}">删除规格</a>
					</td>
				</tr>
				{{endif}}
				{{endforeach}}
			</table>
			<?php
				//去重重复的taid			
				$taid=array_unique($taid);
			 ?>
			<script>
				var taid='<?php echo json_encode($taid) ?>';
				// 把js字符串转化为json对象
				taid=$.parseJSON(taid);
				//循环json
				$.each(taid,function(k,v){
					$('.goodsAttr'+v).first().removeClass('removeSpec btn-info').addClass('addSpec btn-primary').html('增加规格');
				})

			</script>
		<br>


		<div class="alert alert-success" style="background: url({{$goodsData['listpic']}}) no-repeat; background-size:240px 144px;background-position:center right; ">列表图
			<div lab="uploadFile">
				<!-- file表单 -->
			    <input id="f" type="file" multiple="true" >
			    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
			    
			    <div id="uploadList">
			        <ul>

			        </ul>
			    </div>
			</div>
		</div>

		<div class="form-group">
			<label for="exampleInputEmail1">商品内容</label>
			<!--调用百度编辑器-->
			<script id="editor" type="text/plain" style="width:100%;height:500px;" name="content">{{$lnventoryData['content']}}</script>
		    <script>
		        var ue = UE.getEditor('editor');
		    </script>
		</div>

		<div class="alert alert-success" style="background: url({{$goodsData['picture']}}) no-repeat; background-size:240px 144px;background-position:center right;">商品图册
			<div lab="uploadFile">
				<!-- file表单 -->
			    <input id="t" type="file" multiple="true">
			    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
			    
			    <div id="uploadLists">
			        <ul>
			        	<?php if($lnventoryData['picture']): ?>
							{{foreach from="$lnventoryData['picture']" value="$v"}}
						<li>
	                    <img src="{{$v}}" width='100'/>
	                    <input type='hidden' class='piccc' name='picture[]' value="{{$v}}"/><a href='javascript:;' class='self-close'>X</a>
	                    </li>
							{{endforeach}}
	                <?php endif ?>
			        </ul>
			    </div>
			</div>
		</div>


		<button class="btn btn-primary btn-block" type="submit"> 确定 </button>
		</form>
	</body>
	<script type="text/javascript">
        $(function() {
            $('#t').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 10,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"' width='100'/>";
                    li += "<input type='hidden' name='picture[]' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadLists ul").prepend(li);
                }
            });

			//关闭图片
            var i = 10;
            $('.self-close').live('click',function(){
            	//异步删除当前图片
            	var c=$('.piccc').val();alert(c);
            	$.ajax({
					type:"post",
					url:"index.php?m=Admin&c=Goods&a=delpic",
					data:{c:c},
					success:function(p){

					}
				});
                $(this).parent('li').remove();
                i++;
                $('#t').uploadify('settings','uploadLimit',i);
            })
        });
    </script>
    <script type="text/javascript">
        $(function() {
            $('#f').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '{{__PUBLIC__}}/uploadify/uploadify.swf',
                'uploader' : '{{U('upload')}}',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 10,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"' width='100'/>";
                    li += "<input type='hidden' name='listpic' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadList ul").prepend(li);
                }
            });

			//关闭图片
            var i = 10;
            $('#self-close').live('click',function(){
                $(this).parent('li').remove();
                i++;
                $('#f').uploadify('settings','uploadLimit',i);
            })
        });
    </script>
</html>

